package spe.ui.treeprovider;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.Viewer;

import spe.model.spem.MethodContentPackage;
import spe.model.spem.MethodLibrary;
import spe.model.spem.MethodPlugin;
import spe.model.spem.ProcessPackage;
import spe.model.spem.SPEMElement;

/**	@author Dao Anh Vu
 * 	@since 1.0
 * 	This is tree content provider for MethodLibrary treeviewer.
 * 
 * */
public class MethodLibraryTreeContentProvider implements ITreeContentProvider
{
	private final Object[] EMPTY_ARRAY = new Object[0];
	private List<MethodLibrary> libraries = new ArrayList<MethodLibrary>();
	
	public MethodLibraryTreeContentProvider()
	{}
	
	@Override
	public void dispose() 
	{
		libraries.clear();
	}

	@Override
	public void inputChanged(Viewer viewer, Object oldInput, Object newInput) 
	{
		libraries.clear();
		if(newInput instanceof List)
		{
			this.libraries = (List<MethodLibrary>)newInput;
		}
	}

	@Override
	public Object[] getElements(Object inputElement) 
	{
		return libraries.toArray();
	}

	@Override
	public Object[] getChildren(Object parentElement) 
	{
		Object[] childrent;
		if(parentElement instanceof MethodLibrary)
		{
			MethodLibrary lib = (MethodLibrary) parentElement;
			childrent = lib.getChildElements().toArray();
			
			return childrent;
		}
		
		if(parentElement instanceof MethodPlugin)
		{
			MethodPlugin plugin = (MethodPlugin) parentElement;
			return plugin.getChildElements().toArray();
		}
		
		if(parentElement instanceof MethodContentPackage)
		{
			MethodContentPackage mcp = (MethodContentPackage)parentElement;
			return mcp.getOwnedMembers().toArray();
		}
		
		if(parentElement instanceof ProcessPackage)
		{
			ProcessPackage mcp = (ProcessPackage)parentElement;
			//return mcp.get
		}
		
		return EMPTY_ARRAY;
	}

	@Override
	public Object getParent(Object element) 
	{
		if(element instanceof SPEMElement)
			return ((SPEMElement)element).getParent();
		
		return null;
	}

	@Override
	public boolean hasChildren(Object element) 
	{
		return getChildren(element).length > 0;
	}
	
	//testing purpose
	public void addElement(Object element)
	{
		if( (element != null) && (element instanceof MethodLibrary) && !libraries.contains(element))
		{
			libraries.add((MethodLibrary)element);
		}
	}

	
}
